package tw.com.diray;

import tw.com.diary.api.service.FragmentService;
import tw.com.diray.set.RecTopFragment;
import tw.com.diray.set.UnWriteListFragment;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

/***
 * 日記清單
 * 
 * @author Andy
 * 
 */
public class RecView extends FragmentActivity {

	public final static int WRITE_ACTION = 001;

	private Fragment unWriteListFragment;

	private Fragment topFragment;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == WRITE_ACTION && resultCode == RESULT_OK) {
			this.updata();
		}

	}

	public void updata() {
		new QueryList().execute("");
	}

	public class QueryList extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(RecView.this);

		@Override
		protected void onPreExecute() {
			this.dialog.setTitle(getString(R.string.loading));
			this.dialog.show();
		}

		@Override
		protected Void doInBackground(String... params) {

			((FragmentService) RecView.this.topFragment).doQuery();
			((FragmentService) RecView.this.unWriteListFragment).doQuery();

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			this.dialog.dismiss();
			((FragmentService) RecView.this.topFragment).update();
			((FragmentService) RecView.this.unWriteListFragment).update();

		}
	}

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rec_view);

		this.unWriteListFragment = (UnWriteListFragment) getSupportFragmentManager().findFragmentById(R.id.content);
		this.topFragment = (RecTopFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentTop);

		// String[] title = new String[] { "1", "2", "3" };
		//
		// ListView list = (ListView) this.findViewById(R.id.sild_list_view);
		//
		// ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
		// android.R.layout.simple_dropdown_item_1line, title);
		// list.setAdapter(listAdapter);
		this.updata();

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.rec_view_main, menu);

		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

		searchView.setOnQueryTextListener(new OnQueryTextListener() {

			@Override
			public boolean onQueryTextSubmit(String query) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean onQueryTextChange(String newText) {

				// if (newText.length() > 5) {
				//
				// dto.setQuery(newText);
				// service.search(dto, RecView.this);
				//
				// addaptper.notifyDataSetChanged();
				// isSearchNow = true;
				// } else if (isSearchNow) {
				// if (newText.length() == 0) {
				// new QueryList().execute("");
				// isSearchNow = false;
				// }
				// }

				// TODO Auto-generated method stub
				return false;
			}
		});

		searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

		return super.onCreateOptionsMenu(menu);

	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.action_wrirte:

			Intent writeIntent = new Intent(this, CalendarWriteActivity.class);
			this.startActivityForResult(writeIntent, WRITE_ACTION);
			break;

		case R.id.action_modify_passsword:
			// Intent passswordIntent = new Intent(this, ModifyPassword.class);
			//
			// this.startActivity(passswordIntent);

			Intent settingIntent = new Intent(this, DirarySettingActivity.class);
			this.startActivity(settingIntent);
			break;
		}
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

}
